using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 电击Buff - 持续造成闪电伤害，可能造成连锁反应
/// </summary>
public class ShockBuff : BuffBase
{
    private float m_TickInterval = 1.0f; // 每秒造成一次伤害
    private float m_LastTickTime;
    private long m_DamagePerTick;
    private float m_ChainChance; // 连锁概率
    private float m_ChainRange; // 连锁范围

    public ShockBuff(BuffData buffData, Entity caster, Entity target) : base(buffData, caster, target)
    {
        if (buffData is ShockData shockData)
        {
            m_DamagePerTick = shockData.DamagePerTick;
            m_TickInterval = shockData.TickInterval;
            m_ChainChance = shockData.ChainChance;
            m_ChainRange = shockData.ChainRange;
        }
    }

    protected override void OnStart()
    {
        base.OnStart();
        m_LastTickTime = 0f;
        
        // 播放电击特效
        if (BuffData.Effect != null)
        {
            var effect = GameObject.Instantiate(BuffData.Effect, Target.transform);
            effect.transform.localPosition = Vector3.zero;
        }
        
        Logger.Log("ShockBuff", $"{Target.name} 被电击了，每秒造成 {m_DamagePerTick} 点闪电伤害");
    }

    protected override void OnTick(float dt)
    {
        base.OnTick(dt);
        
        // 检查是否到了造成伤害的时间
        if (Time.time - m_LastTickTime >= m_TickInterval)
        {
            ApplyShockDamage();
            m_LastTickTime = Time.time;
        }
    }

    private void ApplyShockDamage()
    {
        if (Target != null && !Target.HealthComponent.IsDead)
        {
            // 造成闪电伤害
            Target.HealthComponent.TakeDamage(m_DamagePerTick, DamageType.Lightning);
            
            Logger.Log("ShockBuff", $"{Target.name} 受到电击伤害: {m_DamagePerTick}");
            
            // 触发伤害事件
            var args = ReferencePool.Acquire<EntityTakeDamageEventArgs>();
            args.Entity = Target;
            args.Damage = m_DamagePerTick;
            args.Attacker = Caster;
            EventHelper.Fire(this, args);
            
            // 检查连锁反应
            if (Random.Range(0f, 1f) < m_ChainChance)
            {
                ChainLightning();
            }
        }
    }

    private void ChainLightning()
    {
        // 查找范围内的其他目标
        Collider2D[] colliders = Physics2D.OverlapCircleAll(Target.transform.position, m_ChainRange);
        
        foreach (var collider in colliders)
        {
            var entity = collider.GetComponent<Entity>();
            if (entity != null && entity != Target && entity != Caster)
            {
                // 对目标施加电击Buff
                if (entity.BuffComponent != null)
                {
                    // 这里需要创建电击Buff数据
                    // var shockData = CreateShockData();
                    // entity.BuffComponent.AddBuff(shockData, Caster, entity);
                }
                
                Logger.Log("ShockBuff", $"电击连锁到{entity.name}");
                break; // 只连锁一次
            }
        }
    }

    protected override void OnFinish()
    {
        base.OnFinish();
        Logger.Log("ShockBuff", $"{Target.name} 电击效果结束");
    }

    protected override void OnRemove()
    {
        base.OnRemove();
        // 清理特效
        var effects = Target.GetComponentsInChildren<ParticleSystem>();
        foreach (var effect in effects)
        {
            if (effect.name.Contains("Shock"))
            {
                GameObject.Destroy(effect.gameObject);
            }
        }
    }

    public override bool IsCanMerge()
    {
        return true; // 电击效果可以叠加，刷新持续时间
    }
}